home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q1082.dms / q1082.adf / src.lzh / Fig / read1_3.c < prev    next >
C/C++ Source or Header  |  1991-07-18  |  10KB  |  456 lines

  1. /* 
  2.  *    FIG : Facility for Interactive Generation of figures
  3.  *
  4.  *    Copyright (c) 1985, 1988 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
  5.  *    January 1985.
  6.  *    1st revision : March 1988.
  7.  *
  8.  *    %W%    %G%
  9. */
  10.  
  11. /*******************************************************************/
  12. /***************       Read version 1.3 format       ***************/
  13. /*******************************************************************/
  14. #include "fig.h"
  15. #include "alloc.h"
  16. #include "object.h"
  17.  
  18. /*******    Fig 1.3 subtype of objects    *******/
  19. #define            DRAW_ELLIPSE_BY_RAD     1
  20. #define            DRAW_ELLIPSE_BY_DIA     2
  21. #define            DRAW_CIRCLE_BY_RAD     3
  22. #define            DRAW_CIRCLE_BY_DIA     4
  23. #define            DRAW_CIRCULAR_ARC    5
  24. #define            DRAW_POLYLINE        6
  25. #define            DRAW_BOX        7
  26. #define            DRAW_POLYGON        8
  27. #define            DRAW_TEXT        9
  28. #define            DRAW_SPLINE        10
  29. #define            DRAW_CLOSEDSPLINE    11
  30. #define            DRAW_COMPOUND        13
  31.  
  32. extern F_arrow        *forward_arrow(), *backward_arrow();
  33. extern int        figure_modified;
  34. extern char        *sys_errlist[];
  35. extern int        sys_nerr, errno;
  36.  
  37. static F_ellipse    *read_ellipseobject();
  38. static F_line        *read_lineobject();
  39. static F_text        *read_textobject();
  40. static F_spline        *read_splineobject();
  41. static F_arc        *read_arcobject();
  42. static F_compound    *read_compoundobject();
  43.  
  44. extern int        line_no;
  45. extern int        num_object;
  46.  
  47. int
  48. read_1_3_objects(fp, obj)
  49. FILE        *fp;
  50. F_compound    *obj;
  51. {
  52.     F_ellipse    *e, *le = NULL;
  53.     F_line        *l, *ll = NULL;
  54.     F_text        *t, *lt = NULL;
  55.     F_spline    *s, *ls = NULL;
  56.     F_arc        *a, *la = NULL;
  57.     F_compound    *c, *lc = NULL;
  58.     int         n;
  59.     int         object, pixperinch, canvaswid, canvasht, coord_sys;
  60.  
  61.     n = fscanf(fp,"%d%d%d%d\n", &pixperinch, &coord_sys, &canvaswid, &canvasht);
  62.     if (n != 4) {
  63.         put_msg("Incorrect format in the first line in input file");
  64.         return(-1);
  65.         }
  66.     obj->nwcorner.x = pixperinch;
  67.     obj->nwcorner.y = coord_sys;
  68.     while (fscanf(fp, "%d", &object) == 1) {
  69.         switch (object) {
  70.         case O_POLYLINE :
  71.             if ((l = read_lineobject(fp)) == NULL) return(-1);
  72.             if (ll)
  73.             ll = (ll->next = l);
  74.             else
  75.             ll = obj->lines = l;
  76.             num_object++;
  77.             break;
  78.         case O_SPLINE :
  79.             if ((s = read_splineobject(fp)) == NULL) return(-1);
  80.             if (ls)
  81.             ls = (ls->next = s);
  82.             else
  83.             ls = obj->splines = s;
  84.             num_object++;
  85.             break;
  86.         case O_ELLIPSE :
  87.             if ((e = read_ellipseobject(fp)) == NULL) return(-1);
  88.             if (le)
  89.             le = (le->next = e);
  90.             else
  91.             le = obj->ellipses = e;
  92.             num_object++;
  93.             break;
  94.         case O_ARC :
  95.             if ((a = read_arcobject(fp)) == NULL) return(-1);
  96.             if (la)
  97.             la = (la->next = a);
  98.             else
  99.             la = obj->arcs = a;
  100.             num_object++;
  101.             break;
  102.         case O_TEXT :
  103.             if ((t = read_textobject(fp)) == NULL) return(-1);
  104.             if (lt)
  105.             lt = (lt->next = t);
  106.             else
  107.             lt = obj->texts = t;
  108.             num_object++;
  109.             break;
  110.         case O_COMPOUND :
  111.             if ((c = read_compoundobject(fp)) == NULL) return(-1);
  112.             if (lc)
  113.             lc = (lc->next = c);
  114.             else
  115.             lc = obj->compounds = c;
  116.             num_object++;
  117.             break;
  118.         default:
  119.             put_msg("Incorrect object code %d", object);
  120.             return(-1);
  121.         } /*  switch */
  122.         } /*  while */
  123.     if (feof(fp))
  124.         return(0);
  125.     else
  126.         return(errno);
  127.     }
  128.  
  129. static F_arc *
  130. read_arcobject(fp)
  131. FILE    *fp;
  132. {
  133.     F_arc    *a;
  134.     int    f, b, h, w, n;
  135.  
  136.     Arc_malloc(a);
  137.     a->type = T_3_POINTS_ARC;
  138.     a->color = BLACK;
  139.     a->depth = 0;
  140.     a->pen = NULL;
  141.     a->for_arrow = NULL;
  142.     a->back_arrow = NULL;
  143.     a->next = NULL;
  144.     n = fscanf(fp, " %d %d %d %f %d %d %d %d %d %f %f %d %d %d %d %d %d\n",
  145.         &a->type, &a->style, &a->thickness, 
  146.         &a->style_val, &a->direction, &f, &b,
  147.         &h, &w, &a->center.x, &a->center.y, 
  148.         &a->point[0].x, &a->point[0].y, 
  149.         &a->point[1].x, &a->point[1].y, 
  150.         &a->point[2].x, &a->point[2].y);
  151.     if (n != 17) {
  152.         put_msg("incomplete arc data");
  153.         free((char*)a);
  154.         return(NULL);
  155.         }
  156.     if (f) {
  157.         a->for_arrow = forward_arrow();
  158.         a->for_arrow->wid = w;
  159.         a->for_arrow->ht = h;
  160.         }
  161.     if (b) {
  162.         a->back_arrow = backward_arrow();
  163.         a->back_arrow->wid = w;
  164.         a->back_arrow->ht = h;
  165.         }
  166.     return(a);
  167.     }
  168.  
  169. static F_compound *
  170. read_compoundobject(fp)
  171. FILE    *fp;
  172. {
  173.     F_arc        *a, *la = NULL;
  174.     F_ellipse    *e, *le = NULL;
  175.     F_line        *l, *ll = NULL;
  176.     F_spline    *s, *ls = NULL;
  177.     F_text        *t, *lt = NULL;
  178.     F_compound    *com, *c, *lc = NULL;
  179.     int         n, object;
  180.  
  181.     Compound_malloc(com);
  182.     com->arcs = NULL;
  183.     com->ellipses = NULL;
  184.     com->lines = NULL;
  185.     com->splines = NULL;
  186.     com->texts = NULL;
  187.     com->compounds = NULL;
  188.     com->next = NULL;
  189.     n = fscanf(fp, " %d %d %d %d\n", &com->nwcorner.x, &com->nwcorner.y,
  190.         &com->secorner.x, &com->secorner.y);
  191.     if (n != 4) {
  192.         put_msg("Incorrect compound object format");
  193.         return(NULL);
  194.         }
  195.     while (fscanf(fp, "%d", &object) == 1) {
  196.         switch (object) {
  197.         case O_POLYLINE :
  198.             if ((l = read_lineobject(fp)) == NULL) { 
  199.             free_line(&l);
  200.             return(NULL);
  201.             }
  202.             if (ll)
  203.             ll = (ll->next = l);
  204.             else
  205.             ll = com->lines = l;
  206.             break;
  207.         case O_SPLINE :
  208.             if ((s = read_splineobject(fp)) == NULL) { 
  209.             free_spline(&s);
  210.             return(NULL);
  211.             }
  212.             if (ls)
  213.             ls = (ls->next = s);
  214.             else
  215.             ls = com->splines = s;
  216.             break;
  217.         case O_ELLIPSE :
  218.             if ((e = read_ellipseobject(fp)) == NULL) { 
  219.             free_ellipse(&e);
  220.             return(NULL);
  221.             }
  222.             if (le)
  223.             le = (le->next = e);
  224.             else
  225.             le = com->ellipses = e;
  226.             break;
  227.         case O_ARC :
  228.             if ((a = read_arcobject(fp)) == NULL) { 
  229.             free_arc(&a);
  230.             return(NULL);
  231.             }
  232.             if (la)
  233.             la = (la->next = a);
  234.             else
  235.             la = com->arcs = a;
  236.             break;
  237.         case O_TEXT :
  238.             if ((t = read_textobject(fp)) == NULL) { 
  239.             free_text(&t);
  240.             return(NULL);
  241.             }
  242.             if (lt)
  243.             lt = (lt->next = t);
  244.             else
  245.             lt = com->texts = t;
  246.             break;
  247.         case O_COMPOUND :
  248.             if ((c = read_compoundobject(fp)) == NULL) { 
  249.             free_compound(&c);
  250.             return(NULL);
  251.             }
  252.             if (lc)
  253.             lc = (lc->next = c);
  254.             else
  255.             lc = com->compounds = c;
  256.             break;
  257.         case O_END_COMPOUND :
  258.             return(com);
  259.         } /*  switch */
  260.         }
  261.     if (feof(fp))
  262.         return(com);
  263.     else {
  264.         put_msg("Format error: %s", sys_errlist[errno]);
  265.         return(NULL);
  266.         }
  267.     }
  268.  
  269. static F_ellipse *
  270. read_ellipseobject(fp)
  271. FILE    *fp;
  272. {
  273.     F_ellipse    *e;
  274.     int        n, t;
  275.  
  276.     Ellipse_malloc(e);
  277.     e->color = BLACK;
  278.     e->angle = 0.0;
  279.     e->depth = 0;
  280.     e->pen = NULL;
  281.     e->area_fill = NULL;
  282.     e->next = NULL;
  283.     n = fscanf(fp," %d %d %d %f %d %d %d %d %d %d %d %d %d\n", 
  284.         &t, &e->style,
  285.         &e->thickness, &e->style_val, &e->direction, 
  286.         &e->center.x, &e->center.y, 
  287.         &e->radiuses.x, &e->radiuses.y, 
  288.         &e->start.x, &e->start.y, 
  289.         &e->end.x, &e->end.y);
  290.     if (n != 13) {
  291.         put_msg("incomplete ellipse data");
  292.         free((char*)e);
  293.         return(NULL);
  294.         }
  295.     if (t == DRAW_ELLIPSE_BY_RAD)
  296.         e->type = T_ELLIPSE_BY_RAD;
  297.     else if (t == DRAW_ELLIPSE_BY_DIA)
  298.         e->type = T_ELLIPSE_BY_DIA;
  299.     else if (t == DRAW_CIRCLE_BY_RAD)
  300.         e->type = T_CIRCLE_BY_RAD;
  301.     else
  302.         e->type = T_CIRCLE_BY_DIA;
  303.     return(e);
  304.     }
  305.  
  306. static F_line *
  307. read_lineobject(fp)
  308. FILE            *fp;
  309. {
  310.     F_line    *l;
  311.     F_point    *p, *q;
  312.     int    f, b, h, w, n, t, x, y;
  313.  
  314.     Line_malloc(l);
  315.     l->color = BLACK;
  316.     l->depth = 0;
  317.     l->pen = NULL;
  318.     l->area_fill = NULL;
  319.     l->for_arrow = NULL;
  320.     l->back_arrow = NULL;
  321.     l->next = NULL;
  322.     l->points = Point_malloc(p);
  323.     n = fscanf(fp, " %d %d %d %f %d %d %d %d %d %d", &t, 
  324.         &l->style, &l->thickness, &l->style_val,
  325.         &f, &b, &h, &w, &p->x, &p->y);
  326.     if (n != 10) {
  327.         put_msg("incomplete line data");
  328.         free((char*)l);
  329.         return(NULL);
  330.         }
  331.     if (t == DRAW_POLYLINE)
  332.         l->type = T_POLYLINE;
  333.     else if (t == DRAW_POLYGON)
  334.         l->type = T_POLYGON;
  335.     else
  336.         l->type = T_BOX;
  337.     if (f) {
  338.         l->for_arrow = forward_arrow();
  339.         l->for_arrow->wid = w;
  340.         l->for_arrow->ht = h;
  341.         }
  342.     if (b) {
  343.         l->back_arrow = backward_arrow();
  344.         l->back_arrow->wid = w;
  345.         l->back_arrow->ht = h;
  346.         }
  347.     for (;;) {
  348.         if (fscanf(fp, " %d %d", &x, &y) != 2) {
  349.         put_msg("incomplete line object");
  350.         free_linestorage(l);
  351.         return(NULL);
  352.         }
  353.         if (x == 9999) break;
  354.         Point_malloc(q);
  355.         q->x = x;
  356.         q->y = y;
  357.         q->next = NULL;
  358.         p->next = q;
  359.         p = q;
  360.         }
  361.     return(l);
  362.     }
  363.  
  364. static F_spline *
  365. read_splineobject(fp)
  366. FILE    *fp;
  367. {
  368.     F_spline    *s;
  369.     F_point        *p, *q;
  370.     int        f, b, h, w, n, t, x, y;
  371.  
  372.     Spline_malloc(s);
  373.     s->color = BLACK;
  374.     s->depth = 0;
  375.     s->pen = NULL;
  376.     s->area_fill = NULL;
  377.     s->for_arrow = NULL;
  378.     s->back_arrow = NULL;
  379.     s->controls = NULL;
  380.     s->next = NULL;
  381.     s->points = Point_malloc(p);
  382.     n = fscanf(fp, " %d %d %d %f %d %d %d %d %d %d", 
  383.             &t, &s->style, &s->thickness, &s->style_val,
  384.             &f, &b,
  385.             &h, &w, &p->x, &p->y);
  386.     if (n != 10) {
  387.         put_msg("incomplete spline data");
  388.         free((char*)s);
  389.         return(NULL);
  390.         }
  391.     if (t == DRAW_CLOSEDSPLINE)
  392.         s->type = T_CLOSED_NORMAL;
  393.     else
  394.         s->type = T_OPEN_NORMAL;
  395.     if (f) {
  396.         s->for_arrow = forward_arrow();
  397.         s->for_arrow->wid = w;
  398.         s->for_arrow->ht = h;
  399.         }
  400.     if (b) {
  401.         s->back_arrow = backward_arrow();
  402.         s->back_arrow->wid = w;
  403.         s->back_arrow->ht = h;
  404.         }
  405.     for (;;) {
  406.         if (fscanf(fp, " %d %d", &x, &y) != 2) {
  407.         put_msg("incomplete spline object");
  408.         free_splinestorage(s);
  409.         return(NULL);
  410.         };
  411.         if (x == 9999) break;
  412.         Point_malloc(q);
  413.         q->x = x;
  414.         q->y = y;
  415.         q->next = NULL;
  416.         p->next = q;
  417.         p = q;
  418.         }
  419.     return(s);
  420.     }
  421.  
  422. static F_text *
  423. read_textobject(fp)
  424. FILE    *fp;
  425. {
  426.     F_text    *t;
  427.     int    n;
  428.     char    buf[128];
  429.  
  430.     Text_malloc(t);
  431.     t->type = T_LEFT_JUSTIFIED;
  432.     t->style = PLAIN;
  433.     t->color = BLACK;
  434.     t->depth = 0;
  435.     t->pen = NULL;
  436.     t->angle = 0.0;
  437.     t->next = NULL;
  438.     n = fscanf(fp," %d %d %d %d %d %d %d %[^\n]", &t->font, 
  439.         &t->size, &t->style, &t->height, &t->length, 
  440.         &t->base_x, &t->base_y, buf);
  441.     if (n != 8) {
  442.         put_msg("incomplete text data");
  443.         free((char*)t);
  444.         return(NULL);
  445.         }
  446.     t->cstring = (char *) malloc((unsigned)(strlen(buf)+1));
  447.     if (t->cstring == NULL) {
  448.         put_msg(Err_mem);
  449.         free((char*) t);
  450.         return(NULL);
  451.         }
  452.     strcpy(t->cstring, buf);
  453.     if (t->size == 0) t->size = 18;
  454.     return(t);
  455.     }
  456.